package edu.lfsy.rabbitmqdemo.controller;

import edu.lfsy.rabbitmqdemo.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 王希辰
 * @date 2022/4/16 23:49
 */
@RestController
@Slf4j
public class SendMsgController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @GetMapping("/sendmsg/{message}")
    public void sendMsg(@PathVariable String message){
        //消息发布exchange:交换器 routingKey:路由键 object:消息内容
        log.info("发送消息消息内容："+message);
        rabbitTemplate.convertAndSend("fanoutExchange","",message);
    }
    @GetMapping("/sendmsg")
    public void sendMsg(){
        User user=new User();
        user.setId(1);
        user.setUsername("张三");
        //消息发布exchange:交换器 routingKey:路由键 object:消息内容
        log.info("发送消息消息内容："+user);
        rabbitTemplate.convertAndSend("fanout_exchange","",user);
    }

    @GetMapping("/sendrmsg")
    public void sendRoutingMsg(){
        rabbitTemplate.convertAndSend("routing_exchange","error_routing_key","object send error message");
        rabbitTemplate.convertAndSend("routing_exchange","info_warning_routing_key","object send info and warning message");
    }
    @GetMapping("/sendtmsg")
    public void sendTopicMsg(){
        //只发送邮件
        //rabbitTemplate.convertAndSend("topic_exchange","info.email","object send error message");
        //只发送短信
        //rabbitTemplate.convertAndSend("topic_exchange","info.sms","topic send error message");
        //同时发送
        rabbitTemplate.convertAndSend("topic_exchange","info.email.sms","topic send error message");


    }

























}
